package net.zjitc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.common.BaseContext;
import net.zjitc.common.R;
import net.zjitc.entity.ShoppingCart;
import net.zjitc.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 请求 URL: http://localhost:8080/shoppingCart/list
     * 请求方法: GET
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 请求 URL: http://localhost:8080/shoppingCart/add
     * 请求方法: POST
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("add ShoppingCart" + shoppingCart);
        //查询
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        if (list.size() == 0) {
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartService.save(shoppingCart);
        } else {
            ShoppingCart cart = list.get(0);
            Integer number = cart.getNumber();
            cart.setNumber(number + 1);
            shoppingCartService.updateById(cart);
        }
        return R.success("添加成功");
    }

    /**
     * 请求 URL: http://localhost:8080/shoppingCart/clean
     * 请求方法: DELETE
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return R.success("清空成功");
    }


    /**
     * 请求 URL: http://localhost:8080/shoppingCart/sub
     * 请求方法: POST
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("sub shopping cart" + shoppingCart);
        //如果这个条数据为1
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(
                shoppingCart.getDishId() != null,
                ShoppingCart::getDishId,
                shoppingCart.getDishId());
        shoppingCartLambdaQueryWrapper.eq(
                shoppingCart.getSetmealId() != null,
                ShoppingCart::getSetmealId,
                shoppingCart.getSetmealId());
        shoppingCartLambdaQueryWrapper.eq(
                ShoppingCart::getUserId,
                BaseContext.getCurrentId()
        );
        ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (one.getNumber() == 1) {
            //删除
            shoppingCartService.removeById(one);
        } else {
            //减一
            Integer number = one.getNumber();
            one.setNumber(number - 1);
            shoppingCartService.updateById(one);
        }
        return R.success("减少成功");
    }
}
